package com.moon.controller;

import com.github.pagehelper.PageInfo;
import com.moon.anno.ReCommit;
import com.moon.dto.ProductVo;
import com.moon.dto.TProduct;
import com.moon.feign.ProductFeign;
import com.moon.utils.IdWorker;
import com.moon.utils.ProductUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    @Qualifier("idWorker1")
    IdWorker idWorker1;

    @Autowired
    ProductFeign productFeign;

    @ReCommit(timeout = 10000)
    @RequestMapping("/insert")
    public String insert(long sellerId, String productName, @RequestParam("fileList[]") List<String> fileList, String productDetail) {
        ProductVo productVo = new ProductVo();
        productVo.setId(idWorker1.nextId());
        productVo.setSellerId(sellerId);
        productVo.setProductName(productName);
        productVo.setProductImgList(fileList);
        productVo.setProductDetail(productDetail);
        productVo.setStatus(0);
        return productFeign.insert(ProductUtils.getString(productVo));
    }

    @RequestMapping("/selectAll")
    public PageInfo<TProduct> selectAll(Integer currentPage, Integer pageSize) {
        return productFeign.selectAll(currentPage, pageSize);
    }

    @RequestMapping("/delete")
    public String delete(long productId) {
        return productFeign.delete(productId);
    }

    @RequestMapping("/update")
    public String update(long id, String productName, @RequestParam("fileList[]") List<String> fileList, String productDetail, Integer status) {
        ProductVo productVo = new ProductVo();
        productVo.setId(id);
        productVo.setProductName(productName);
        productVo.setProductImgList(fileList);
        productVo.setProductDetail(productDetail);
        productVo.setStatus(status);
        TProduct product = ProductUtils.getString(productVo);
        return productFeign.update(product);
    }
}
